package com.gonsin.venus.meeting.logic.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gonsin.venus.meeting.bean.TopicBean;
import com.gonsin.venus.meeting.po.TopicSearchPo;
import org.apache.ibatis.annotations.*;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

import java.util.List;

/**
 * 议题数据库处理
 */
@Mapper
public interface TopicMapper extends BaseMapper<TopicBean> {

    String TOPIC_PAGEABLE = "" +
            "<if test=\"!pageable.sort.toString().equals('UNSORTED')\">" +
            "    order by" +
            "    <foreach item=\"item\" index=\"index\" collection=\"pageable.sort.orders\" separator=\",\">" +
            "        t.${item.property} ${item.direction}" +
            "    </foreach>" +
            "</if>" +
            "limit ${pageable.page * pageable.size},${pageable.size} ";

    @Results(id = "TopicBean", value = {

            @Result(column = "topic_key", property = "topicKey"),
            @Result(column = "meetingkey", property = "meetingkey"),
    })
    @Select("<script>" +
            "select " +
            "   t.* " +
            "from gs_meeting_topic as t " +
            "where 1=1 " +
            "<if test = \"searchPo.meetingkey != null and meetingkey != ''\">" +
            "   and t.meetingkey = #{searchPo.meetingkey} " +
            "</if>" +
            "<if test = \"searchPo.topicKeys != null and searchPo.topicKeys.size() &gt; 0\">" +
            "   and t.topic_key in " +
            "   <foreach collection=\"searchPo.topicKeys\" item=\"item\" index=\"index\" separator=\",\" open=\"(\" close=\")\">" +
            "       #{item}" +
            "   </foreach>" +
            "</if>" +
//            "group by t.topic_key " +
            TOPIC_PAGEABLE +
            "</script>")

    /**
     * 查询议题
     */
    List<TopicBean> searchTopic(@Param("searchPo") TopicSearchPo searchPo,
                                @Param("pageable") Pageable pageable);

    /**
     * 查询议题数量
     */
    @Select("<script>" +
            "select " +
            "COUNT(distinct t.topic_key)" +
            "from gs_meeting_topic as t " +
            "where 1=1 " +
            "<if test = \"searchPo.meetingkey != null and meetingkey != ''\">" +
            "   and t.meetingkey = #{searchPo.meetingkey} " +
            "</if>" +
            "<if test = \"searchPo.topicKeys != null and searchPo.topicKeys.size() &gt; 0\">" +
            "   and t.topic_key in " +
            "   <foreach collection=\"searchPo.topicKeys\" item=\"item\" index=\"index\" separator=\",\" open=\"(\" close=\")\">" +
            "       #{item}" +
            "   </foreach>" +
            "</if>" +
            "</script>")
    int countTopic(@Param("searchPo") TopicSearchPo searchPo);
}
